package net.cloudsun.graph.puzzle;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 图片切割
 *
 * @author Ryan
 * @since 3.0
 */
public final class ImageSplit {

    private ImageSplit() {}

    public static void split() {
        File file = new File("image.jpeg");
        try (FileInputStream inputStream = new FileInputStream(file)){
            final BufferedImage image = ImageIO.read(inputStream);
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    final BufferedImage subimage = image.getSubimage(i * 60, j * 60, 60,  60);
                    final File output = new File("images/" + i + "-" + j + ".png");
                    if (output.exists() || output.createNewFile()) {
                        ImageIO.write(subimage, "png", output);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
